package action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;


import beans.Admin;
import beans.Agent;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.AdminDao;

@SuppressWarnings("serial")
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {

	private Admin admin = new Admin();

	private int adminID;
	private List<Admin> admins;
	private String returnType;
	Map<String, Object> message;

	public Admin getModel() {
		return admin;
	}

	private AdminDao adminDao = new AdminDao();

	/**
	 * 获取管理员信息
	 * 
	 * @return
	 */
	public String getAdminById() {
		admin = adminDao.getAdminById(adminID);
		return "success";
	}

	/**
	 * 编辑管理员
	 * 
	 * @return
	 */
	public String editAdmin() {
		admin.setAdminId(adminID);
		adminDao.edit(admin);
		return "success";
	}

	/**
	 * 删除管理员
	 * 
	 * @return
	 */
	public String deleteAdmin() {
		admin.setAdminId(adminID);
		adminDao.delete(admin);
		return "success";
	}

	/**
	 * Model-driven to fetch data So the method name have to be getModel()
	 * 
	 * @return
	 */

	public String login() {

		Agent admin1 = adminDao.get(admin.getUsername(), admin.getPassword());
		if (returnType == null) {
			if (admin1 != null) {
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("admin", admin1);
				session.setMaxInactiveInterval(3600);	
				return "success";
			} else {
				return "fail";
			}
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			if (admin1 != null) {
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("admin", admin1);
				session.setMaxInactiveInterval(3600);	
				message.put("result", 0);
			} else {
				message.put("result", 1);
			}
			return "jsonResult";
		} else {
			return "fail";
		}

	}

	public String registerAdmin() {
		adminDao.save(admin);
		return "success";
	}

	public String getAll() {

		admins = adminDao.getAll();
		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			message.put("result", 0);
			message.put("admins", admins);
			return "jsonSuccess";
		}
		return "success";

	}

	public List<Admin> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public Map<String, Object> getMessage() {
		return message;
	}

	public void setMessage(Map<String, Object> message) {
		this.message = message;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public int getAdminID() {
		return adminID;
	}

	public void setAdminID(int adminID) {
		this.adminID = adminID;
	}

}
